package com.mingzhi.forum.controller;

import com.mingzhi.forum.common.AppResult;
import com.mingzhi.forum.common.ResultCode;
import com.mingzhi.forum.exception.ApplicationException;
import com.mingzhi.forum.model.Board;
import com.mingzhi.forum.service.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "板块接口API")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    @Value("${mingzhi-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;

    //API描述
    @Operation(summary = "查询前N个板块信息")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        //打印日志
        log.info("indexBoardNum = " + indexBoardNum);
        //调用service层 service已经做过校验了
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        //返回给前端结果
        return AppResult.success(boards);
    }

    @Operation(summary = "查询全部的板块信息")
    @GetMapping("/allNormal")
    public AppResult<List<Board>> allNormal(){
        //调用service层
        List<Board> boards = boardService.selectAllNormal();
        //返回给前端结果
        return AppResult.success(boards);
    }

    @Operation(summary = "根据id查询板块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@Parameter(description = "板块ID") @RequestParam("id") @NonNull Long id){
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
